/*************************************************
Description:智能家居--客厅界面的实现，包括客厅的灯，温湿度
**************************************************/
#include "parlourwidget.h"
#include "ui_Parlourwidget.h"

#include <QDebug>
#include <QSound>

#include "control.h"
#include "myapp.h"
#include <QJsonParseError>
#include <QJsonValue>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>

//预设温度值，湿度值
int set_parlour_tempture = Myapp::setTempValue;
int set_parlour_humidity = Myapp::setHumValue;

Parlourwidget::Parlourwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Parlourwidget)
{
    ui->setupUi(this);

    this->initForm();
    this->init();
    this->initConnect();
}

Parlourwidget::~Parlourwidget()
{
    delete ui;
}

void Parlourwidget::initForm()
{
    this->setToolButtonStyle(ui->tbn_temp_add,tr("tempture_add"),
                             ":/images/module/temp_add.png");
    this->setToolButtonStyle(ui->tbn_temp_sub,tr("tempture_sub"),
                             ":/images/module/temp_sub.png");
    this->setToolButtonStyle(ui->tbn_hum_add,tr("humidity_add"),
                             ":/images/module/temp_add.png");
    this->setToolButtonStyle(ui->tbn_hum_sub,tr("humidity_sub"),
                             ":/images/module/temp_sub.png");

    m_styleOn = "QLabel{border-image:url(:/images/module/lamp_on.png);border:0px;}";
    m_styleOff = "QLabel{border-image:url(:/images/module/lamp_off.png);border:0px;}";
    f_styleOn = "QLabel{border-image:url(:/images/module/fan_on.png);border:0px;}";
    f_styleOff = "QLabel{border-image:url(:/images/module/fan_off.png);border:0px;}";
    a_styleOn = "QLabel{border-image:url(:/images/module/air_on.png);border:0px;}";
    a_styleOff = "QLabel{border-image:url(:/images/module/air_off.png);border:0px;}";


    //setLightStyle(m_styleOff);

    ui->pbn_light_main->setFocusPolicy(Qt::NoFocus);
    ui->pbn_light_top->setFocusPolicy(Qt::NoFocus);
    ui->pbn_light_help->setFocusPolicy(Qt::NoFocus);

    //温湿度预设值设置
    ui->label_set_hum->setText(QString::number(set_parlour_tempture));
    ui->label_set_temp->setText(QString::number(set_parlour_humidity));

    //根据配置文件状态信息，同步各个灯的状态信息
    if (Myapp::parlour_led_main == true){
        ui->pbn_light_main->SetCheck(true);
        ui->label_light_main->setStyleSheet(m_styleOn);
    }else{
        ui->pbn_light_main->SetCheck(false);
        ui->label_light_main->setStyleSheet(m_styleOff);
    }
    if (Myapp::parlour_led_top == true){
        ui->pbn_light_top->SetCheck(true);
        ui->label_light_top->setStyleSheet(f_styleOn);
    }else{
        ui->pbn_light_top->SetCheck(false);
        ui->label_light_top->setStyleSheet(f_styleOff);
    }
    if (Myapp::parlour_led_help == true){
        ui->pbn_light_help->SetCheck(true);
        ui->label_light_help->setStyleSheet(a_styleOn);
    }else{
        ui->pbn_light_help->SetCheck(false);
        ui->label_light_help->setStyleSheet(a_styleOff);
    }
}

void Parlourwidget::init()
{
}

//初始化信号和槽的连接
void Parlourwidget::initConnect()
{
    //改变灯的状态
    connect(ui->pbn_light_main,SIGNAL(clicked()),
            this,SLOT(slot_lightMain()));
    connect(ui->pbn_light_top,SIGNAL(clicked()),
            this,SLOT(slotLighttop()));
    connect(ui->pbn_light_help,SIGNAL(clicked()),
            this,SLOT(slotLighthelp()));
    connect(this,SIGNAL(signalControl(int)),
            this,SLOT(slotControl(int)));
}

void Parlourwidget::setToolButtonStyle(QToolButton *tbn,
                                       const QString &text,
                                       const QString iconName)
{
    //设置显示的文本
    tbn->setText(text);
    //tbn->setFont(QFont("文泉驿雅黑",10,QFont::Normal));

    tbn->setAutoRaise(true);
    //设置按钮图标
    tbn->setIcon(QPixmap(QString("%1").arg(iconName)));
    tbn->setIconSize(QSize(35,35));
    //设置文本在图标下边
    tbn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
}

//设置灯的样式
void Parlourwidget::setLightStyle(const QString &styleName)
{
    if (styleName == "m_styleOn")
    {
        ui->label_light_main->setStyleSheet(m_styleOn);
        ui->label_light_top->setStyleSheet(f_styleOn);
        ui->label_light_help->setStyleSheet(a_styleOn);
        ui->pbn_light_main->SetCheck(true);
        ui->pbn_light_top->SetCheck(true);
        ui->pbn_light_help->SetCheck(true);
    }else
    {
        ui->label_light_main->setStyleSheet(m_styleOff);
        ui->label_light_top->setStyleSheet(f_styleOff);
        ui->label_light_help->setStyleSheet(a_styleOff);
        ui->pbn_light_main->SetCheck(false);
        ui->pbn_light_top->SetCheck(false);
        ui->pbn_light_help->SetCheck(false);
    }

}

//刷新传感器的值,用于改变传感器值时刷新显示
void Parlourwidget::refreshSensorDefaultValue()
{
    ui->label_set_temp->setText(QString::number(set_parlour_tempture,10));
    ui->label_set_hum->setText(QString::number(set_parlour_humidity,10)+
                               "%");;
}

void Parlourwidget::setLedTurnStatus(quint8 status)
{
    if (LED_ON == status)
    {
        ui->label_light_main->setStyleSheet(m_styleOn);
        ui->label_light_top->setStyleSheet(f_styleOn);
        ui->label_light_help->setStyleSheet(a_styleOn);
        ui->pbn_light_main->SetCheck(true);
        ui->pbn_light_top->SetCheck(true);
        ui->pbn_light_help->SetCheck(true);
    }else if (LED_OFF == status)
    {
        ui->label_light_main->setStyleSheet(m_styleOff);
        ui->label_light_top->setStyleSheet(f_styleOff);
        ui->label_light_help->setStyleSheet(a_styleOff);
        ui->pbn_light_main->SetCheck(false);
        ui->pbn_light_top->SetCheck(false);
        ui->pbn_light_help->SetCheck(false);
    }

}

//客厅主灯状态改变发送相应的信号
void Parlourwidget::slot_lightMain()
{
    if (ui->pbn_light_main->GetCheck())
    {
        ui->label_light_main->setStyleSheet(m_styleOn);
        QSound::play(":/sounds/switch.wav");
        emit signalControl(110);// 110代表开灯
    }
    else
    {
        ui->label_light_main->setStyleSheet(m_styleOff);
        QSound::play(":/sounds/switch.wav");
        emit signalControl(100);// 100代表关灯
    }

}

//客厅风扇状态改变发送相应的信号
void Parlourwidget::slotLighttop()
{
    if (ui->pbn_light_top->GetCheck())
    {
        ui->label_light_top->setStyleSheet(f_styleOn);
        QSound::play(":/sounds/switch.wav");
        emit signalControl(210);// 210代表开风扇
    }
    else
    {
        ui->label_light_top->setStyleSheet(f_styleOff);
        QSound::play(":/sounds/switch.wav");
        emit signalControl(200);// 200代表关风扇
    }

}

//客厅空调状态发生改变时发送相应的信号
void Parlourwidget::slotLighthelp()
{
    if (ui->pbn_light_help->GetCheck())
    {
        ui->label_light_help->setStyleSheet(a_styleOn);
        QSound::play(":/sounds/switch.wav");
        emit signalControl(310);//31代表开空调
    }
    else
    {
        ui->label_light_help->setStyleSheet(a_styleOff);
        QSound::play(":/sounds/switch.wav");
        emit signalControl(300);//30代表关空调
    }

}

//更新显示温度值
void Parlourwidget::slotUpdate(QString data)
{
    //解析json
    int key;
    QString val;

    QObjectList objList;
    objList = this->children();
    QJsonParseError jsonerror;
    QJsonDocument doc = QJsonDocument::fromJson(data.toLatin1(), &jsonerror);
    QJsonObject object = doc.object();
    if(object.value("type")==1)
    {

     //判断data的数据类型是不是数组
        switch(object.value("data").type())
        {
            case QJsonValue::Object:
           {
                 QJsonObject obj = object.value("data").toObject();
                 key = obj.value("key").toInt();
                 val = obj.value("val").toVariant().toString();
                 if(val.contains("."))
                 {
                     QStringList list =val.split(".");
                     val=list[0];
                 }

                 //判断需要哪个控件处理
                 if(key==101)//modbus温度
                 {
                     ui->label_cur_temp->setText(val);
                     qDebug()<<"parlour temp:"<<val;
                 }
                 else if(key==102)//modbus湿度
                 {
                     ui->label_cur_hum->setText(val);
                     qDebug()<<"parlour hum:"<<val;
                 }
                 else if(key==301||key==302)//STM32火焰/STM32电池含量
                 {
                     //给bedroom发送信号
                     emit signaltobed(key,val);
                 }

           }

            case QJsonValue::Array:
           {
                QJsonArray arr = object.value("data").toArray();
                for(int i = 0; i < arr.count(); i++)
                {
                    QJsonObject obj = arr.at(i).toObject();
                    key = obj.value("key").toInt();
                    val = obj.value("val").toVariant().toString();

                    //判断需要哪个控件处理
                    if(key==101)//modbus温度
                    {
                        ui->label_cur_temp->setText(val);
                        qDebug()<<"parlour temp:"<<val;
                    }
                    else if(key==102)//modbus湿度
                    {
                        ui->label_cur_hum->setText(val);
                        qDebug()<<"parlour hum:"<<val;
                    }
                    else if(key==301||key==302)//STM32火焰/STM32电池含量
                    {
                        //给bedroom发送信号
                        emit signaltobed(key,val);
                    }

                }
           }
            default:
           {
             break;
           }
        }
    }
}


void Parlourwidget::slotControl(int num)
{

   //组json包
    QJsonObject jsObject;
    jsObject.insert("type",QJsonValue(2));  //控制指令
    QJsonObject tmpObject;

   qDebug()<<"num=:"<<num<<endl;
    if(num==110)//modbus开灯
    {
    tmpObject.insert("key",105);//key值
    tmpObject.insert("val","1");//val值
    }
    else if(num==100)//modbus关灯
    {
    tmpObject.insert("key",105);//key值
    tmpObject.insert("val","0");//val值
    }
    else if(num==210)//modbus开风扇
    {
    tmpObject.insert("key",106);//key值
    tmpObject.insert("val","1");//val值
    }
    else if(num==200)//modbus关风扇
    {
    tmpObject.insert("key",106);//key值
    tmpObject.insert("val","0");//val值
    }
    else if(num==910)//开灯(STM32 左卧室灯借用)
    {
    tmpObject.insert("key",303);//key值
    tmpObject.insert("val","1");//val值
    }
    else if(num==900)//关灯(STM32 右卧室灯借用)
    {
    tmpObject.insert("key",303);//key值
    tmpObject.insert("val","0");//val值
    }
    else if(num==810)//开灯(STM32 左卧室灯借用)
    {
    tmpObject.insert("key",304);//key值
    tmpObject.insert("val","1");//val值
    }
    else if(num==800)//关灯(STM32 右卧室灯借用)
    {
    tmpObject.insert("key",304);//key值
    tmpObject.insert("val","0");//val值
    }
    else if(num==310)//modbus开空调
    {
    tmpObject.insert("key",103);//key值
    tmpObject.insert("val","1");//val值
    }
    else if(num==300)//mosbus关空调
    {
    tmpObject.insert("key",103);//key值
    tmpObject.insert("val","0");//val值
    }
    else//空调温度设置
    {
    tmpObject.insert("key",104);//key值
    tmpObject.insert("val",QJsonValue(num).toVariant().toString());//val值
    }

    jsObject.insert("data",QJsonValue(tmpObject));
    QString str = QString(QJsonDocument(jsObject).toJson(QJsonDocument::Compact));
    emit signaltomqtt(str);

}


//add current parlour tempture
void Parlourwidget::on_tbn_temp_add_clicked()
{
    //get current tempture
    QString temp = ui->label_set_temp->text();
    QString new_temp;
    if (temp.toInt() < 99)
    {
        new_temp = QString::number(temp.toInt() + 1);
    }
    ui->label_set_temp->setText(new_temp);
#if QDEBUG
    qDebug()<<"current tempture:"<<temp;
    qDebug()<<"add tempture:"<<new_temp<<endl;
#endif
    emit signalControl(new_temp.toInt());//空调温度+

}

//sub current tempture
void Parlourwidget::on_tbn_temp_sub_clicked()
{
    //get current tempture
    QString temp = ui->label_set_temp->text();
    QString new_temp;
    if (temp.toInt() > 0)
    {
        new_temp = QString::number(temp.toInt() - 1);
    }
    ui->label_set_temp->setText(new_temp);
#if QDEBUG
    qDebug()<<"current tempture:"<<temp;
    qDebug()<<"sub tempture:"<<new_temp<<endl;
#endif
    emit signalControl(new_temp.toInt());//空调温度-
    //send temptuer
   // emit signalSendTemptureValue(MODULE_DS18B20,new_temp);
}

//add current humidity
void Parlourwidget::on_tbn_hum_add_clicked()
{
    //get current humidity
    QString humi = ui->label_set_hum->text();
    QString new_humi;
    if (humi.toInt() < 99)
    {
        new_humi = QString::number(humi.toInt() + 1);
    }
    ui->label_set_hum->setText(new_humi);
#if QDEBUG
    qDebug()<<"current humidity:"<<humi;
    qDebug()<<"sub humidity:"<<new_humi<<endl;
#endif

}

//sub current humidity
void Parlourwidget::on_tbn_hum_sub_clicked()
{
    //get current humidity
    QString humi = ui->label_set_hum->text();
    QString new_humi;
    if (humi.toInt() > 0)
    {
        new_humi = QString::number(humi.toInt() - 1);
    }
    ui->label_set_hum->setText(new_humi);
#if QDEBUG
    qDebug()<<"current humidity:"<<humi;
    qDebug()<<"sub humidity:"<<new_humi<<endl;
#endif

}

//系统设置处预设温度
void Parlourwidget::slotChangeTempSetValue(const int &value)
{
    ui->label_set_temp->setText(QString::number(value));
}

void Parlourwidget::slotChangeHumSetValue(const int &value)
{
    ui->label_set_hum->setText(QString::number(value));
}
